using System.Windows.Forms;
using RollManager.Library;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;

namespace RollManager.Forms.MainPanels
{
    public partial class AdminToolPanel : UserControl, MainPanel
    {
        private List<HistoryExtended> m_completeHistory;
        private Dictionary<string, List<HistoryExtended>> m_currentValues = new Dictionary<string, List<HistoryExtended>>();

        public AdminToolPanel()
        {
            InitializeComponent();
        }

        public override void Refresh()
        {
            base.Refresh();
            
            m_completeHistory = Database.GetHistoryExtended();
            refreshHistoryView( m_completeHistory );
        }

        public void Save()
        {
        }

        private void columnBox_SelectedIndexChanged( object sender, System.EventArgs e )
        {
            m_currentValues.Clear();

            // Get Type of History field from name of column without spaces.
            FieldInfo field = typeof( HistoryExtended ).GetField( ( (string) columnBox.SelectedItem ).Replace( " ", "" ) );

            // Get value of selected filter column for each history item.
            List<string> values = new List<string>();
            foreach( HistoryExtended history in m_completeHistory )
            {
                string value = field.GetValue( history ).ToString();
                if( m_currentValues.ContainsKey( value ) )
                {
                    m_currentValues[ value ].Add( history );
                }
                else
                {
                    List<HistoryExtended> histories = new List<HistoryExtended>();
                    histories.Add( history );
                    m_currentValues[ value ] = histories;
                    values.Add( value );
                }
            }

            values.Sort();
            valueBox.DataSource = values;
        }

        private void refreshHistoryView( List<HistoryExtended> histories )
        {
            historyView.Rows.Clear();

            foreach( HistoryExtended history in histories )
            {
                historyView.Rows.Add( history.Date, history.DevLevel, history.Action, history.SubAction,
                                      history.UserName, history.Result, history.Context );
            }
            if (historyView.SortOrder != SortOrder.None)
            {
                historyView.Sort(historyView.SortedColumn, (historyView.SortOrder == SortOrder.Ascending ? ListSortDirection.Ascending : ListSortDirection.Descending));
            }
            else
            {
                historyView.Sort(DateColumn, ListSortDirection.Descending);
            }
        }

        private void valueBox_SelectedIndexChanged( object sender, System.EventArgs e )
        {
            refreshHistoryView( m_currentValues[ (string) valueBox.SelectedItem ] );
        }
    }
}